package com.jdd.modules.sdk.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @Description 项目启动解压获取驱动文件
 * @Author wanglei
 * @Date 2021-04-21 17:18
 * @version 1.0
 **/
@Slf4j
public class JarDecompressUtils {

    /**
     * @Description 解压获取驱动文件
     * @Author wanglei
     * @Date 2021-04-21 17:19
     * @Param [servletContext]
     * @version 1.0
     * @return void
     **/
    public static synchronized boolean jarDecompressForDrive() {
        try {
            String jarPath = System.getProperty("user.dir") + File.separator + "park-station.jar";
            decompress(jarPath);
            return true;
        } catch (Exception e){
            log.info("解压获取驱动文件出错" + e.getMessage());
        }

        return false;
    }


    /**
     * @Description 解压jar包
     * @Author wanglei
     * @Date 2021-04-21 11:35
     * @Param [fileName, outputPath]
     * @version 1.0
     * @return void
     **/
    public static synchronized void decompress(String fileName) {
        //不同操作系统驱动路径
        String windowsPath = System.getProperty("user.dir") + File.separator + "lib-dll" + File.separator  + "win64" ;
        String linuxPath = System.getProperty("user.dir") + File.separator + "lib-dll" + File.separator  + "linux" ;

        //jar文件是否存在
        if(!new File(fileName).exists()){
            return;
        }

        if (!windowsPath.endsWith(File.separator)) {
            windowsPath += File.separator;
        }

        if (!linuxPath.endsWith(File.separator)) {
            linuxPath += File.separator;
        }

        // windows系统驱动路径
        File windowsDir = new File(windowsPath);
        if (!windowsDir.exists()) {
            windowsDir.mkdirs();
        }

        // linux系统驱动路径
        File linuxDir = new File(linuxPath);
        if (!linuxDir.exists()) {
            linuxDir.mkdirs();
        }

        JarFile jf = null;

        try {
            jf = new JarFile(fileName);

            //便利jar包
            for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements(); ) {
                JarEntry je = e.nextElement();

                //获取文件名
                File file = new File(je.getName());

                //获取驱动文件
                if (je.getName().endsWith(".so") || je.getName().endsWith(".dll")) {

                    String outFileName = je.getName().endsWith(".so") ?  (linuxPath + file.getName()) : (windowsPath + file.getName());

                    //驱动文件是否存在
                    if(new File(outFileName).exists()){
                        continue;
                    }

                    //保持驱动文件
                    InputStream in = jf.getInputStream(je);
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outFileName)));

                    byte[] buffer = new byte[2048];
                    int nBytes = 0;
                    while ((nBytes = in.read(buffer)) > 0) {
                        out.write(buffer, 0, nBytes);
                    }

                    out.flush();
                    out.close();
                    in.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jf != null) {
                try {
                    jf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}